//
//  MKTalentPoolViewController.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/7.
//  Copyright © 2017年 cc.moko. All rights reserved.
//  人才库页面

import UIKit

class MKTalentPoolViewController: MKBaseViewController,UICollectionViewDelegate,UICollectionViewDataSource,MKWaterFallLayoutProtocol {

    var navigationBar:MKHomeNavigationBar!
    var collectionView:UICollectionView!
    var filterParams:[String:Any]!
    var mtbSearchedArray:[MKMtbSearchedModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.makeUI()
        self.filterParams = [:]
        self.filterParams["page"] = 1
        self.filterParams["perpage"] = 16
        self.filterParams["np"] = 0
        print(self.filterParams)
        self.mtbSearchedArray = []
        self.initData()
    }
    func initData() -> Void {
        
        MKNetWorkTool.postRequest(url: "mtb/search/person", params: self.filterParams, success: { (response, responseObject) in
            let model = MKSearchMtbResponse.deserialize(from: responseObject)
            if (model?.resultcode?.isEqual("ok"))!{
                if let searchMtbModel = model?.info{
                    print(searchMtbModel)
                    self.mtbSearchedArray? += searchMtbModel.searchList!
                    self.collectionView.reloadData()
                }
            }else{
                // 成功的其他情况的展示
            }
            
        }) { (response, error) in
            
            
        }
    }
    
    func makeUI() -> Void {
        self.navigationBar = MKHomeNavigationBar.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 64))
        self.view.addSubview(self.navigationBar)
        
        let layout:MKWaterFallLayout = MKWaterFallLayout.init()
        layout.rowMargin = 5
        layout.columnMargin = 5
        layout.columnCount = 2
        layout.edge = UIEdgeInsets.init(top: 8, left: 5, bottom: 8, right: 5)
        
        layout.delegate = self as MKWaterFallLayoutProtocol
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 64, width: ScreenWidth, height: ScreenHeight - 64), collectionViewLayout: layout)
        self.collectionView.backgroundColor = UIColorFromRGB(rgbValue: 0xf0f0f0, A: 1)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(MKTalentPoolCell.classForCoder(), forCellWithReuseIdentifier: "MKTalentPoolCell")
         self.collectionView.register(MKTalentPoolMtbCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "MKTalentPoolMtbCollectionViewCell")
        
        self.view.addSubview(self.collectionView)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        if(self.mtbSearchedArray?.count == 0){
//            return 0
//        }else{
            return (self.mtbSearchedArray?.count)! + 1
//        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if(indexPath.item == 0){
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MKTalentPoolMtbCollectionViewCell", for: indexPath)
            return cell
        }
        else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MKTalentPoolCell", for: indexPath) as! MKTalentPoolCell
            let model:MKMtbSearchedModel = self.mtbSearchedArray![indexPath.item - 1] 
            cell.searchedModel = model
            return cell
        }
    }
    
    func waterLayout(waterLayout: MKWaterFallLayout, indexPath heightForRowAtIndexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
        if(heightForRowAtIndexPath.row == 0){
            return 42
        }
        else
        {
            return 244
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
